package com.ocom.aiot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.aiot.dao.IDaoOtaPushInfo;
import com.ocom.aiot.service.AppVerInfoService;
import com.ocom.aiot.service.OtaPushInfoService;
import com.ocom.aiot.service.OtaVerInfoService;
import com.ocom.common.entity.aiot.EntityOtaPushInfo;
import com.ocom.common.entity.aiot.EntityOtaVerInfo;
import com.ocom.common.request.aiot.OtaPushDetailVo;
import com.ocom.common.request.aiot.OtaPushVo;
import com.ocom.common.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OtaPushInfoServiceImpl extends ServiceImpl<IDaoOtaPushInfo, EntityOtaPushInfo> implements OtaPushInfoService {

	@Autowired
	AppVerInfoService appVerInfoService;

	@Autowired
	OtaVerInfoService otaVerInfoService;

	@Override
	public PageInfo  getOtaList(Integer page, Integer size, String deviceNo) {
		QueryWrapper<EntityOtaPushInfo> queryWrapper =new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		if(!CommonUtil.isNull(deviceNo)){
			queryWrapper.eq("device_no",deviceNo);
		}
		PageHelper.startPage(page, size);
		List<EntityOtaPushInfo> list=this.list(queryWrapper);
		PageInfo<EntityOtaPushInfo> pageInfo=new PageInfo<>(list);
		return pageInfo;
	}

	//获取升级
	@Override
	public EntityOtaVerInfo getOtaPush(String deviceNo, String appVer) {
		QueryWrapper<EntityOtaPushInfo> queryWrapper =new QueryWrapper<>();
		queryWrapper.eq("device_no",deviceNo);
		EntityOtaPushInfo entityOtaPushInfo=this.getOne(queryWrapper);
		if(entityOtaPushInfo==null){
			EntityOtaPushInfo entityOtaPushInfo1 = new EntityOtaPushInfo();
			entityOtaPushInfo1.setDeviceNo(deviceNo);
			entityOtaPushInfo1.setAppVer(appVer);
			this.save(entityOtaPushInfo1);
			return null;
		}
		UpdateWrapper<EntityOtaPushInfo> updateWrapper =new UpdateWrapper<>();
		updateWrapper.eq("device_no",deviceNo);
		updateWrapper.set("app_ver",appVer);
		this.update(updateWrapper);

		if(entityOtaPushInfo.getPushVer() == null || entityOtaPushInfo.getPushVer().equals(appVer)){
			return null;
		}
		return  otaVerInfoService.selectAppVerInfo(entityOtaPushInfo.getPushVer());
//		return appVerInfoService.selectAppVerInfo(entityOtaPushInfo.getPushVer());
	}

	@Override
	public void pushVer(OtaPushVo otaPushVo) {
		for (OtaPushDetailVo otaPushDetailVo : otaPushVo.getList()){
			EntityOtaPushInfo entityOtaPushInfo = new EntityOtaPushInfo();
			entityOtaPushInfo.setDeviceNo(otaPushDetailVo.getDeviceNo());
			entityOtaPushInfo.setPushVer(otaPushDetailVo.getAppVer());

			UpdateWrapper<EntityOtaPushInfo> updateWrapper =new UpdateWrapper<>();
			updateWrapper.eq("device_no",otaPushDetailVo.getDeviceNo());
			updateWrapper.set("push_ver",otaPushDetailVo.getAppVer());

			this.saveOrUpdate(entityOtaPushInfo,updateWrapper);
		}
	}
}
